﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.驾驶员管理
{
    public partial class AddDriver : System.Web.UI.Page
    {
        DriverBLL driverBLL = new DriverBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAddDriver();
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void BindAddDriver()
        {
            //获取添加的驾驶员信息
            string name = txt_name.Text.Trim();
            string sex =radio_sex.SelectedValue;
            string phone = txt_phone.Text.Trim();
            string idcard = txt_number.Text.Trim();
            string teamid = txt_truckteam.Text.Trim();
            string  state =radio_state.SelectedValue;
            string remark = txt_ream.Text.Trim();
            string birth = txt_brider.Text.Trim();
            string checktime = txt_addtime.Text.Trim();
            //判断车队id
            if (!Regex.IsMatch(teamid, @"^[0-9]*[1-9][0-9]*$"))
            {
                lbl_Msg.Text = "您输入的车队ID不正确，id为正整数！";
                return;
            }
            //判断电话号码格式
            if (!Regex.IsMatch(phone, @"^1[35789]\d{9}$"))
            {
                lbl_Msg.Text = "您输入的手机号码格式不正确！";
                return;
            }
            //判断身份证号格式 身份证号码
            // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
            if (!Regex.IsMatch(idcard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$"))
            {
                lbl_Msg.Text = "您输入的身份证号码格式不正确！";
                return;
            }
            //判断时间格式 生日
            if (!Regex.IsMatch(birth, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                lbl_Msg.Text = "您输入的生日格式不正确！";
                return;
            }
            //判断时间格式 添加时间
            if (!Regex.IsMatch(checktime, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                lbl_Msg.Text = "您输入的添加时间格式不正确！";
                return;
            }
            DriverMODEL model = new DriverMODEL()
            {
                Name = name,
                Sex =Convert.ToInt32(sex),
                Brith = Convert.ToDateTime(birth),
                Phone = phone,
                IDCard = idcard,
                FK_TeamID = Convert.ToInt32(teamid),
                State =Convert.ToInt32(state),
                Remark = remark,
                CheckInTime = Convert.ToDateTime(checktime)
            };
            int result = driverBLL.Getdriver_add(model);
            if (result == 1)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');location.href='DriverInfoList.aspx'</script>");
            }
            else if (result == -1)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('用户名已存在，请勿重复添加！')</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加失败！')</script>");
            }

        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_update_Click(object sender, EventArgs e)
        {
            //跳转到驾驶员信息页面
            BindAddDriver();
        }
    }
}